﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public partial class patriota_Lista : System.Web.UI.Page
{
    DataTable dt = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) {
            gv_datos.DataSource = (DataTable)DB.getDatosPatriota2("");
            gv_datos.DataBind();
           

             filldeptos();
            
        }

        
    }

    private void fillmunicipio()
    {
        ListItem li = new ListItem();
        li.Text = "Seleccionar";
        li.Value = "0";
        cb_depto.Items.Add(li);
        DataTable dt = DB.getMunicipiosGT(0);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            li = new ListItem();
            li.Text = dt.Rows[i]["nombre_muni"].ToString();
            li.Value = dt.Rows[i]["id_muni"].ToString();
            cb_municipio.Items.Add(li);

        }
        cb_depto.SelectedIndex = 0;
        
    }

    private void filldeptos()
    {
        ListItem li = new ListItem();
        li.Text = "Seleccionar";
        li.Value = "0";
        cb_depto.Items.Add(li);
        DataTable dt= DB.getDepartamentosGT();
        for (int i = 0; i < dt.Rows.Count; i++ ) {
            li = new ListItem();
            li.Text = dt.Rows[i]["nombre_dep"].ToString();
            li.Value = dt.Rows[i]["id_dep"].ToString();
            cb_depto.Items.Add(li);
        
        }
        cb_depto.SelectedIndex = 0;
        
        

    }
   
    

    

    
    protected void cb_depto_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cb_depto.SelectedIndex!=0){
            cb_municipio.Items.Clear();
       
            int depto= int.Parse(cb_depto.SelectedValue.ToString());
            ListItem li = new ListItem();
            li.Text = "Seleccionar";
            li.Value = "0";
            cb_municipio.Items.Add(li);
            DataTable dt = DB.getMunicipiosGT(depto);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                li = new ListItem();
                li.Text = dt.Rows[i]["nombre_muni"].ToString();
                li.Value = dt.Rows[i]["id_muni"].ToString();
                cb_municipio.Items.Add(li);

            }
           
        
        }
    }
    
    protected void check_checked(object sender, EventArgs e)
    {
        CheckBox ch = (CheckBox)sender;
        if (ch.Checked == true)
        {
            GridViewRow objFila = (GridViewRow)ch.Parent.Parent;
            TextBox tb = (TextBox)objFila.FindControl("tb_votos");
            tb.Enabled = false;
        }
        else {
            GridViewRow objFila = (GridViewRow)ch.Parent.Parent;
            TextBox tb = (TextBox)objFila.FindControl("tb_votos");
            tb.Enabled = true;
        
        }
    }
    protected void bt_buscar_Click(object sender, ImageClickEventArgs e)
    {
        string where = " Where "; int bandera = 0;
        if (cb_depto.SelectedIndex != 0) { 
            where+= "(departamento.id_dep = "+cb_depto.SelectedValue.ToString()+")";
            bandera = 1;
        }
        if (cb_municipio.SelectedIndex !=0){
            if (bandera == 1)
            {
                where += " and (municipios.id_muni = " + cb_municipio.SelectedValue.ToString() + ")";
            }
            else {
                where += "  (municipios.id_muni = " + cb_municipio.SelectedValue.ToString() + ")";
                bandera = 1;
            }
            
        }

        if (!tb_telefono.Text.Equals("")){
            if (bandera == 1)
            {
                where += " AND (celula.celular = '" + tb_telefono.Text.Trim() + "' )";
            }
            else
            {
                where += " (celula.celular = '" + tb_telefono.Text.Trim() + "' )";
                bandera = 1;
            }
        
        
        }

        if (!tb_nombre.Text.Equals("")) {
            if (bandera == 1)
            {
                where += " AND (Upper(celula.nombre) = '" + tb_nombre.Text.Trim().ToUpper() + "' )";
            }
            else
            {
                where += " (Upper(celula.nombre) = '" + tb_nombre.Text.Trim().ToUpper() + "' )";
                bandera = 1;
            }
        
        
        }


        gv_datos.DataSource = (DataTable)DB.getDatosPatriota2(where);
        gv_datos.DataBind(); 
        tb_nombre.Text = "";
        tb_telefono.Text = "";
        cb_depto.SelectedIndex = 0;
        cb_municipio.Items.Clear();
       
       
    }
    protected void gv_datos_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType== DataControlRowType.DataRow){
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Left;
        
        }
         
    }
}